<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>registe</title>
    <link rel="stylesheet" href="../statics/css/bootstrap.min.css">
    <link rel="stylesheet" href="../statics/HTMLCSS/registe.css">
    <!-- <meta name="viewport" content="width=device-width,initial-scale=1" />
</head> -->

<body background="../statics/Photo/富士山.jpg" style="background-repeat: no-repeat;
background-size: 100% 100%;
background-attachment: fixed;">
    <div class="container">

        <p id="title">Sign your life📕</p>
        <div class="col">
            <div class="alert alert-danger alert-dismissible fade show invisible text-center" style="width: 42rem;"
                role="alert">
                <strong>提示：</strong><label for="" id="msg">555</label>
                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
        </div>
        <form id="registerForm" action="/registerDone" method="POST">
            <p id="number">
                Number:&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="username" id="username"
                    style="border-top: none;border-left: none;border-right: none; border-bottom:solid 1px;width:300px;height: 30px;font-size: 20px;">
            </p>
            <p id="password">
                Password:&nbsp;&nbsp;&nbsp;&nbsp;<input type="password" name="passwords" placeholder="8-16code"
                    id="passwords" maxlength="16"
                    style="border-top: none;border-left: none;border-right: none; border-bottom:solid 1px;width:300px;height: 30px;font-size: 20px;">
            </p>
            <p id="surePassword">
                AgainPwd:&nbsp;&nbsp;&nbsp;&nbsp<input type="password" placeholder="8-16 code" name="surePasswords"
                    id="surePasswords" maxlength="16"
                    style="border-top: none;border-left: none;border-right: none; border-bottom:solid 1px;width:300px;height: 30px;font-size: 20px;">
            </p>

            <button type="button" id="registe" class="btn btn-primary" onclick="register()" style="height:60px; width:210px;">Register</button>

            <button type="button" id="cancelRegiste" class="btn btn-primary" onclick="cancelRegister()" style="height:60px; width:210px;">CancelRegister</button>
        </form>

    </div>

    <script src="../statics/js/jquery-3.6.0.min.js"></script>
    <script src="../statics/js/bootstrap.min.js"></script>

    <script>

        function register() {
            let username = $('#username').val();
            let passwords = $('#passwords').val();
            let surePasswords = $('#surePasswords').val();
            if (username.length > 0 && passwords.length > 0 && passwords === surePasswords) {
                // ajax请求
                $.ajax({
                    url: "/registerDone",
                    type: "POST",
                    data: $('#registerForm').serialize(),
                    dataType: "JSON",
                    success: function (res) {
                        console.log(res);
                        if (res.code === 200) {
                            window.location.href = "/login";
                        } else {
                            alert_fn(res.msg);
                        }

                    },
                    error: function (msg) {
                        alert_fn('An unknown error has occurred!');
                    }
                })
            } else {// 验证不通过，提示相关信息，并且阻止提交
                alert_fn();
                return false;
            }
        }
        function alert_fn(msg) {
            msg = msg || 'The password cannot be empty, the password should be consistent';
            // 替换提示的内容
            $('#msg').text(msg);
            // 移除不显示的class，则alert将显示
            $('.invisible').removeClass('invisible');
            // 三秒中后，给alert增加不显示的class，则alert重新隐藏
            setTimeout(() => {
                $('.alert').addClass('invisible');
            }, 3000);
        }

        function cancelRegister() {
            window.location.href = "/login";
        }

    </script>
</body>

</html>